<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left">
        <div class="brand-container">
          <img src="../assets/images/logo.png" alt="Logo" class="brand-logo" />
          <h1 class="brand-name">懂车之家</h1>
          <p class="brand-slogan">专业的汽车销售管理系统</p>
        </div>
        <div class="features">
          <div class="feature-item">
            <el-icon class="feature-icon"><DataAnalysis /></el-icon>
            <span>销售数据分析</span>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><Van /></el-icon>
            <span>汽车库存管理</span>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><User /></el-icon>
            <span>客户关系管理</span>
          </div>
        </div>
      </div>
      
      <div class="login-right">
        <div class="login-card">
          <div class="login-header">
            <h2>欢迎回来</h2>
            <p>请登录您的账号</p>
          </div>
          
          <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
            <el-form-item prop="credential">
              <el-input 
                v-model="loginForm.credential" 
                placeholder="用户名/手机号/邮箱"
                :prefix-icon="User"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="密码"
                :prefix-icon="Lock"
                show-password
              />
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                :loading="loading" 
                class="login-button" 
                @click="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>
            
            <div class="login-options">
              <router-link to="/register" class="register-link">
                没有账号？立即注册
              </router-link>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../store/user'
import { ElMessage } from 'element-plus'
import { User, Lock, DataAnalysis, Van } from '@element-plus/icons-vue'

// 状态
const loginForm = reactive({
  credential: '',
  password: ''
})

const rules = {
  credential: [
    { required: true, message: '请输入用户名/手机号/邮箱', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)
const loading = ref(false)
const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 方法
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      
      try {
        const { success, message } = await userStore.login(
          loginForm.credential,
          loginForm.password
        )
        
        if (success) {
          ElMessage.success('登录成功')
          
          // 如果有重定向参数，则跳转到对应页面，否则跳转到首页
          const redirectPath = route.query.redirect || '/'
          router.push(redirectPath)
        } else {
          ElMessage.error(message || '登录失败')
        }
      } catch (error) {
        ElMessage.error('登录过程中发生错误')
        console.error(error)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #f0f2f5, #e6e9ef);
  overflow: hidden;
  position: relative;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><rect fill="rgba(64, 158, 255, 0.03)" width="50" height="50" x="0" y="0"></rect><rect fill="rgba(64, 158, 255, 0.03)" width="50" height="50" x="50" y="50"></rect></svg>');
  opacity: 0.5;
  z-index: 0;
}

.login-content {
  display: flex;
  width: 900px;
  height: 550px;
  background-color: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  z-index: 1;
  animation: fadeIn 0.8s ease-in-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #409EFF, #3a8ee6);
  color: white;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 60%);
  animation: rotate 30s linear infinite;
  z-index: 0;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.brand-container {
  position: relative;
  z-index: 1;
  text-align: center;
  margin-top: 40px;
}

.brand-logo {
  width: 80px;
  height: 80px;
  margin-bottom: 20px;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.1));
}

.brand-name {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-slogan {
  font-size: 16px;
  opacity: 0.9;
}

.features {
  position: relative;
  z-index: 1;
  margin-top: 40px;
}

.feature-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  backdrop-filter: blur(5px);
  transition: all 0.3s ease;
}

.feature-item:hover {
  background-color: rgba(255, 255, 255, 0.2);
  transform: translateX(5px);
}

.feature-icon {
  font-size: 24px;
  margin-right: 15px;
}

.login-right {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

.login-card {
  width: 100%;
  max-width: 360px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  font-size: 28px;
  color: var(--text-primary);
  margin-bottom: 10px;
  font-weight: 600;
}

.login-header p {
  font-size: 16px;
  color: var(--text-secondary);
}

.login-form {
  margin-top: 20px;
}

:deep(.el-input__wrapper) {
  padding: 12px 15px;
  border-radius: 8px;
  box-shadow: 0 0 0 1px var(--border-color) inset;
  transition: all 0.3s ease;
  outline: none;
}

:deep(.el-input--password .el-input__wrapper) {
  outline: none;
  box-shadow: 0 0 0 1px var(--border-color) inset;
}

:deep(.el-input--password .el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--primary-color) inset;
  outline: none;
}

:deep(.el-input--password .el-input__wrapper:focus) {
  outline: none;
  box-shadow: none;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px var(--primary-color) inset;
}

:deep(.el-input) {
  outline: none;
}

:deep(.el-input:focus) {
  outline: none;
  box-shadow: none;
}

:deep(.el-input__inner) {
  border: none;
  outline: none;
}

:deep(.el-input.is-focus) {
  outline: none;
  box-shadow: none;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--primary-color) inset;
  outline: none;
}

/* 移除输入框焦点时的默认轮廓 */
:deep(.el-input__wrapper:focus) {
  outline: none;
  box-shadow: none;
}

:deep(.el-input__inner:focus) {
  outline: none;
  box-shadow: none;
}

/* 通用焦点样式重置 */
:deep(*:focus) {
  outline: none !important;
  box-shadow: none !important;
}

:deep(.el-input__inner) {
  height: 24px;
  font-size: 15px;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

.login-button {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  background: linear-gradient(135deg, #409EFF, #3a8ee6);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  background: linear-gradient(135deg, #66b1ff, #409EFF);
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
  transform: translateY(-2px);
}

.login-options {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.register-link {
  font-size: 15px;
  color: var(--primary-color);
  text-decoration: none;
  transition: all 0.3s ease;
  position: relative;
}

.register-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background-color: var(--primary-color);
  transition: width 0.3s ease;
}

.register-link:hover::after {
  width: 100%;
}

/* 响应式布局 */
@media (max-width: 992px) {
  .login-content {
    width: 90%;
    max-width: 900px;
    flex-direction: column;
    height: auto;
  }
  
  .login-left {
    padding: 30px;
  }
  
  .features {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;
  }
  
  .feature-item {
    flex: 1;
    margin-right: 10px;
    padding: 10px;
    flex-direction: column;
    text-align: center;
  }
  
  .feature-item:last-child {
    margin-right: 0;
  }
  
  .feature-icon {
    margin-right: 0;
    margin-bottom: 10px;
  }
}

@media (max-width: 768px) {
  .login-content {
    width: 95%;
    max-width: 450px;
  }
  
  .login-left {
    display: none;
  }
  
  .login-right {
    padding: 30px;
  }
  
  .login-card {
    max-width: 100%;
  }
}
</style>